import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:magic_car/common/nodata.dart';
import 'package:magic_car/jiance/pre_test_report.dart';
import 'package:magic_car/user/userPrivate.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:magic_car/main.dart';
import 'package:package_info/package_info.dart';
import 'package:toast/toast.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';
class MessagePage extends StatefulWidget {
  MessagePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MessagePage> {
  List messagelist = [];
  initState() {
    super.initState();
  
    loadData();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Color.fromRGBO(245, 245, 245, 1),
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title:Text("消息中心"),
      ),
      body: messagelist.length==0?Nodata():ListView.builder(
        itemCount: messagelist.length,
        itemBuilder: (BuildContext context,int index){
        return messageitem(messagelist[index]);
      }),
      
    );
  }


    //加载数据
  loadData() async {
    // if(keyword!=""){
    //   print(keyword);
    // if(end)return;
    // }
    
    Map<String, String> map = new Map();
    SharedPreferences pref = await SharedPreferences.getInstance();
    map["uid"] = pref.getString('uid');
    
    
    var role = pref.getString("role");
    map["type"]=role;
    // map["page"] = page.toString();
    // map["keyword"] = keyword;
    print(map);

    HttpHelper.post(HttpHelper.API_Message, (data) async {
      if (data != null) {
        print(data);
        print(data["info"].length.toString());
        if (data["info"].length == 0) {
          Toast.show('暂无更多数据', context);
        } else {
          setState(() {
            messagelist = data["info"];
          });
          
        }
      }
    }, params: map);
  }
  Widget messageitem(data){
    return Container(
      margin: EdgeInsets.symmetric(vertical: 10),
      child: Column(
        children: [
          Center(
            child: Text(data["createtime"],style: TextStyle(color: Colors.grey,fontSize: 12)),
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
            padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
            decoration: BoxDecoration(
              color: Colors.white,
              
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(data["title"],style: TextStyle(fontWeight: FontWeight.bold,),),
                Container(height: 5,),
                Text(data["content"])
              ],
            ),
          ),
        ],
      ),
    );
  }
}